/*
 * Copyright (c) 2013 by Chances.
 * $CVSHeader$
 * $Author$
 * $Date$
 * $Revision$
 */
package chances.epg.navigator.service;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerMapping;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.config.EpgSiteContext;
import chances.epg.navigator.context.AccessContext;

/**
 * 请求上下文工厂类.
 *
 * @author <a href="mailto:libf@chances.com.cn">libf</a>
 */
@Component
public class AccessContextFactory {
	private static final Logger logger = EpgLogFactory.getSystemLogger();

	@Value("${resource.root.path:/resource}")
	String resourceRootPath;

	/**
	 * 访问上下文.
	 * 
	 * @param request
	 * @return
	 */
	public AccessContext createAccessContext(HttpServletRequest request) {
		AccessContext context = new AccessContext();
		this.initContext(context, request);
		this.initContextFromURI(context, request);
		return context;
	}

	/**
	 * init context
	 * 
	 * @param context
	 * @param request
	 */
	private void initContext(AccessContext context, HttpServletRequest request) {
		// 请求号
		String sessionId = request.getRequestedSessionId();
		String tranId = sessionId + "_" + RandomStringUtils.randomNumeric(6);
		context.setTranId(tranId);

	}

	/**
	 * 访问路径中的参数.
	 * 
	 * @param context
	 * @param request
	 */
	@SuppressWarnings("unchecked")
	private void initContextFromURI(AccessContext context, HttpServletRequest request) {
		Map<String, String> uriTemplateVariables = (Map<String, String>) request
				.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
		if (uriTemplateVariables == null) {
			logger.error("Not found uri template variables.");
			return;
		}

		// uri variables
		String siteCode = uriTemplateVariables.get("siteCode");
		if (StringUtils.isNotBlank(siteCode)) {
			context.setSiteCode(siteCode);
			context.setEpgSiteContext(EpgSiteContext.get(request.getServletContext(), siteCode));
		}

		String bizCode = uriTemplateVariables.get("bizCode");
		if (StringUtils.isNotBlank(bizCode)) {
			context.setBizCode(bizCode);
		}

		String containerType = uriTemplateVariables.get("containerType");
		if (StringUtils.isNotBlank(containerType)) {
			context.setContainerType(containerType.toLowerCase());
		}

		String containerCode = uriTemplateVariables.get("containerCode");
		if (StringUtils.isNotBlank(containerCode)) {
			context.setContainerCode(containerCode);
		}

		String itemId = uriTemplateVariables.get("itemId");
		if (StringUtils.isNotBlank(itemId)) {
			context.setItemId(Long.parseLong(itemId));
		}

		String itemIndex = request.getParameter("pi");
		if (StringUtils.isNotBlank(itemIndex)) {
			context.setItemIndex(itemIndex);
		}

		String parentContentType = uriTemplateVariables.get("parentContentType");
		if (StringUtils.isNotBlank(parentContentType)) {
			context.setParentContentType(parentContentType.toLowerCase());
		}

		String parentContentCode = uriTemplateVariables.get("parentContentCode");
		if (StringUtils.isNotBlank(parentContentCode)) {
			context.setParentContentCode(parentContentCode);
		}

		String contentType = uriTemplateVariables.get("contentType");
		if (StringUtils.isNotBlank(contentType)) {
			context.setContentType(contentType.toLowerCase());
		}

		String contentCode = uriTemplateVariables.get("contentCode");
		if (StringUtils.isNotBlank(contentCode)) {
			context.setContentCode(contentCode);
		}

	}

	
}
